﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO.Ports;
using SerialPortHelperLib;
using XChipTool.Enties;
using System.Threading;
using XChip_Common;
using System.Configuration;
using BinAnalysisHelp;

namespace XChipTool
{
    public partial class XChip_CRL : DevExpress.XtraEditors.XtraUserControl
    {
        public SerialPort sPortTemp;
        carBackLineTemplateManager carBackLineTemplateM;
        List<carBackLineTemplate> carBackLineTemplateList;
        //是否执行事件的标志，非人为触法，不要执行事件情况下
        private bool evenMask = false;
        //记录是否使用USB设备读写
        public bool IsUsbOpen = false;
        //记录是否使用I2C设备读写
        public bool m_open = false;
        public parameter_exportManager Pe_dal { get; set; }
        private KeyEventArgs keyEnter = new KeyEventArgs(Keys.Enter);//用来手动触发文本框下发配置
        private bool isWrite = false;
        private addressregisterManager aBll;
        private List<addressregister> XChipCrl;
        public XChip_CRL()
        {
            InitializeComponent();
        }
        public XChip_CRL(SerialPort _sPortTemp, addressregisterManager _aBll)
        {
            sPortTemp = _sPortTemp;
            aBll = _aBll;
            XChipCrl = aBll.GetList(c => c.ChipType == ConfigurationManager.AppSettings["xc151_ChipType"] && (c.Classify == "CRL" || c.Classify == "Common"));

            InitializeComponent();
        }

        #region 读写该模块寄存器封装
        private long GetCRLRegister(string nameType)
        {
            if (!sPortTemp.IsOpen && !IsUsbOpen && !m_open)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                return -1;
            }
            if (XChipCrl.Where(c => c.NameType == nameType).Count() > 0)
                return SerialSend.ReadRegister(Convert.ToUInt32(XChipCrl.Where(c => c.NameType == nameType).First().Address, 16), sPortTemp);
            else { DevExpress.XtraEditors.XtraMessageBox.Show($"未找到{nameType}"); return -1; }

            //if (XChipCrl.Exists(c => c.NameType.ToLower() == nameType.ToLower()))
            //{
            //    var model = XChipCrl.Find(c => c.NameType.ToLower() == nameType.ToLower());
            //    var Val = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
            //    if (model.ControlType.Contains("1"))
            //    {
            //        return BitsHelp.GetBitsCheckedToBool(model.Bits, model.Hex, Val.ToString("X2"));
            //    }
            //    else
            //    {
            //        return BitsHelp.GetBitsToUint(model.Bits, model.Hex, Val.ToString("X2"));
            //    }
            //}
            //else
            //{
            //    DevExpress.XtraEditors.XtraMessageBox.Show($"未找到{nameType}"); return -1;
            //}
        }
        private void SetCRLRegister(addressregister model, ulong add, bool isCheck = true)
        {
            if (evenMask) return;
            if (!sPortTemp.IsOpen && !IsUsbOpen && !m_open)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                return;
            }
            //if (isRead) return;
            //if (XChipCrl.Where(c => c.NameType == nameType).Count() > 0)
            //{
            //    SerialSend.WriteRegister(Convert.ToUInt32(XChipCrl.Where(c => c.NameType == nameType).First().Address, 16), add, sPortTemp);
            //}
            //else { DevExpress.XtraEditors.XtraMessageBox.Show($"未找到{nameType}");  }

            if (IsUsbOpen)
            {
                if (null != BinHelp.usbList)
                {
                    uint pVule = 0x0;
                    var isbool = false;
                    if (IntPtr.Size == 8)
                    {
                        isbool = XChipUSB.UsbRegisterRead64(BinHelp.CurrentSize, ref pVule, true);
                        SerialSend.Sendser("R:0x" + (BinHelp.CurrentSize).ToString("X2"), pVule.ToString("X2"));


                    }
                    else
                    {
                        isbool = XChipUSB.UsbRegisterRead86(BinHelp.CurrentSize, ref pVule, true);
                        SerialSend.Sendser("R:0x" + (BinHelp.CurrentSize).ToString("X2"), pVule.ToString("X2"));
                    }
                    var chang = (int)(pVule >> 16); // 使用右移16位操作来获取高16位  
                    var kuang = (int)(pVule & 0xFFFF); // 使用位与操作来获取低16位  

                    BinHelp.SstBinUsbUpdateData(ref BinHelp.BinList, model.Address, add.ToString("X"), chang, kuang);
                }
            }
            else if (m_open)
            {

            }
            else
            {
                if (!sPortTemp.IsOpen)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                    return;
                }

                if (null != BinHelp.lspList)
                { //编写Bin更新数据
                    var temp = SerialSend.ReadRegister(BinHelp.BootSettingIdx, sPortTemp);
                    BinHelp.SetBinUpdateData(ref BinHelp.BinList, Convert.ToUInt32(model.Address, 16).ToString("X2"), add.ToString("X"), (uint)temp);
                }
            }

            SerialSend.WriteRegister(Convert.ToUInt32(model.Address, 16), add, sPortTemp);

        }

        private void SetCRLRegister(string nameType, ulong add)
        {
            if (evenMask) return;
            if (!sPortTemp.IsOpen && !IsUsbOpen && !m_open)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                return;
            }

            if (IsUsbOpen)
            {
                if (null != BinHelp.usbList && IsUsbOpen)
                {
                    uint pVule = 0x0;
                    var isbool = false;
                    if (IntPtr.Size == 8)
                    {
                        isbool = XChipUSB.UsbRegisterRead64(BinHelp.CurrentSize, ref pVule, true);
                        SerialSend.Sendser("R:0x" + (BinHelp.CurrentSize).ToString("X2"), pVule.ToString("X2"));
                    }
                    else
                    {
                        isbool = XChipUSB.UsbRegisterRead86(BinHelp.CurrentSize, ref pVule, true);
                        SerialSend.Sendser("R:0x" + (BinHelp.CurrentSize).ToString("X2"), pVule.ToString("X2"));
                    }
                    var chang = (int)(pVule >> 16); // 使用右移16位操作来获取高16位  
                    var kuang = (int)(pVule & 0xFFFF); // 使用位与操作来获取低16位  

                    BinHelp.SstBinUsbUpdateData(ref BinHelp.BinList, XChipHelp.XChipCADJ.Where(c => c.NameType.ToLower() == nameType.ToLower()).First().Address, add.ToString("X"), chang, kuang);
                }
            }
            else if (m_open)
            {

            }
            else
            {
                if (null != BinHelp.lspList && XChipCrl.Where(c => c.NameType.ToLower() == nameType.ToLower()).Count() > 0)
                { //编写Bin更新数据
                    var temp = SerialSend.ReadRegister(BinHelp.BootSettingIdx, sPortTemp);
                    BinHelp.SetBinUpdateData(ref BinHelp.BinList, XChipCrl.Where(c => c.NameType.ToLower() == nameType.ToLower()).First().Address, add.ToString("X"), (uint)temp);
                }

            }
            if (XChipCrl.Where(c => c.NameType.ToLower() == nameType.ToLower()).Count() > 0)
            {
                SerialSend.WriteRegister(Convert.ToUInt32(XChipCrl.Where(c => c.NameType.ToLower() == nameType.ToLower()).First().Address, 16), add, sPortTemp);
            }
        }

        #endregion

        private void comboBoxEdit1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!sPortTemp.IsOpen && !IsUsbOpen && !m_open)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                return;
            }
            if (carBackLineTemplateM == null)
            {
                carBackLineTemplateM = new carBackLineTemplateManager();
            }
            if (carBackLineTemplateList == null)
            {
                carBackLineTemplateList = carBackLineTemplateM.GetList();
            }
            switch (comboBoxEdit1.SelectedIndex)
            {
                case 0:
                    foreach (var item in carBackLineTemplateList)
                    {
                        SerialSend.WriteRegister(Convert.ToUInt32(item.Addr, 16), Convert.ToUInt16(item.value0, 16), sPortTemp);
                    }
                    break;
                case 1:
                    foreach (var item in carBackLineTemplateList)
                    {
                        SerialSend.WriteRegister(Convert.ToUInt32(item.Addr, 16), Convert.ToUInt16(item.value1, 16), sPortTemp);
                    }
                    break;
                //case 2:
                //    foreach (var item in carBackLineTemplateList)
                //    {
                //        SerialSend.WriteRegister(Convert.ToUInt32(item.Addr, 16), Convert.ToUInt16(item.value2, 16), sPortTemp);
                //    }
                //    break;
                default:
                    break;
            }
        }

        private void btn_GetIspSetting_Click(object sender, EventArgs e)
        {
            if (!sPortTemp.IsOpen && !IsUsbOpen && !m_open)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                return;
            }
            #region 文本框赋值
            //Left_d1.Text = GetCRLRegister("Left_d1").ToString("x");
            //Left_d2.Text = GetCRLRegister("Left_d2").ToString("x");
            //Left_width1_h.Text = GetCRLRegister("Left_width1 [10:8]").ToString("x")+ GetCRLRegister("Left_width1 [7:0]").ToString("x");
            ////txt_Left_width1_l.Text = GetCRLRegister("Left_width1 [7:0]").ToString("x");
            //Left_width2_h.Text = GetCRLRegister("Left_width2 [10:8]").ToString("x")+ GetCRLRegister("Left_width2 [7:0]").ToString("x");
            ////txt_Left_width2_l.Text = GetCRLRegister("Left_width2 [7:0]").ToString("x");
            //Left_height_h.Text = GetCRLRegister("Left_height [10:8]").ToString("x") + GetCRLRegister("Left_height [7:0]").ToString("x");
            ////txt_Left_height_l.Text = GetCRLRegister("Left_height [7:0]").ToString("x");
            //Left_x_h.Text = GetCRLRegister("Left_x [11:8]").ToString("x")+ GetCRLRegister("Left_x [7:0]").ToString("x");
            ////txt_Left_x_l.Text = GetCRLRegister("Left_x [7:0]").ToString("x");
            //Left_y_h.Text = GetCRLRegister("Left_y [10:8]").ToString("x")+ GetCRLRegister("Left_y [7:0]").ToString("x");
            ////txt_Left_y_l.Text = GetCRLRegister("Left_y [7:0]").ToString("x");
            //Left_blank_h.Text = GetCRLRegister("Left_blank [9:8]").ToString("x")+ GetCRLRegister("Left_blank [7:0]").ToString("x");
            ////txt_Left_blank_l.Text = GetCRLRegister("Left_blank [7:0]").ToString("x");

            //Right_d1.Text = GetCRLRegister("Right_d1").ToString("x");
            //Right_d2.Text = GetCRLRegister("Right_d2").ToString("x");
            //Right_width1_h.Text = GetCRLRegister("Right_width1 [10:8]").ToString("x")+ GetCRLRegister("Right_width1 [7:0]").ToString("x");
            ////txt_Right_width1_l.Text = GetCRLRegister("Right_width1 [7:0]").ToString("x");
            //Right_width2_h.Text = GetCRLRegister("Right_width2 [10:8]").ToString("x")+ GetCRLRegister("Right_width2 [7:0]").ToString("x");
            ////txt_Right_width2_l.Text = GetCRLRegister("Right_width2 [7:0]").ToString("x");
            //Right_height_h.Text = GetCRLRegister("Right_height [10:8]").ToString("x")+ GetCRLRegister("Right_height [7:0]").ToString("x");
            ////txt_Right_height_l.Text = GetCRLRegister("Right_height [7:0]").ToString("x");
            //Right_x_h.Text = GetCRLRegister("Right_x [11:8]").ToString("x") + GetCRLRegister("Right_x [7:0]").ToString("x");
            ////txt_Right_x_l.Text = GetCRLRegister("Right_x [7:0]").ToString("x");
            //Right_y_h.Text = GetCRLRegister("Right_y [10:8]").ToString("x")+ GetCRLRegister("Right_y [7:0]").ToString("x");
            ////txt_Right_y_l.Text = GetCRLRegister("Right_y [7:0]").ToString("x");
            //Right_blank_h.Text = GetCRLRegister("Right_blank [9:8]").ToString("x")+ GetCRLRegister("Right_blank [7:0]").ToString("x");
            ////txt_Right_blank_l.Text = GetCRLRegister("Right_blank [7:0]").ToString("x");

            //Zone1_y.Text = GetCRLRegister("Zone1_y").ToString("x");
            //Zone1_u.Text = GetCRLRegister("Zone1_u").ToString("x");
            //Zone1_v.Text = GetCRLRegister("Zone1_v").ToString("x");
            //Zone2_y.Text = GetCRLRegister("Zone2_y").ToString("x");
            //Zone2_u.Text = GetCRLRegister("Zone2_u").ToString("x");
            //Zone2_v.Text = GetCRLRegister("Zone2_v").ToString("x");
            //Zone3_y.Text = GetCRLRegister("Zone3_y").ToString("x");
            //Zone3_u.Text = GetCRLRegister("Zone3_u").ToString("x");
            //Zone3_v.Text = GetCRLRegister("Zone3_v").ToString("x");
            //Zone4_y.Text = GetCRLRegister("Zone4_y").ToString("x");
            //Zone4_u.Text = GetCRLRegister("Zone4_u").ToString("x");
            //Zone4_v.Text = GetCRLRegister("Zone4_v").ToString("x");

            evenMask = true;
            WaitBefore.Start("读取中", "正在加载中", 10);
            List<string> NameList = new List<string>();

            for (int i = 0; i < XChipCrl.Count; i++)
            {
                if (XChipCrl[i].NameType.Contains("trigger_timer"))
                {

                }
                var type = AppHelper.NameTypeToString(XChipCrl[i].NameType);
                if (NameList.Exists(c => c.ToLower() == (type)))
                {
                    continue;
                }
                ////加上单选框的_en
                //if (xChipHDRAe[i].ControlType == "1")
                //{
                //    type = xChipHDRAe[i].NameType;
                //}
                var col = Controls_Find(type);

                foreach (var item in col)
                {
                    if (item is TextEdit)
                    {
                        var nList = XChipCrl.Where(c => c.NameType.ToLower().Contains(item.Name.ToString().ToLower()) && c.ControlType == "0").ToList();
                        if (item.Name.ToLower() == type.ToLower())
                        {
                            nList = XChipCrl.Where(c => c.NameType.ToLower() == (item.Name.ToString().ToLower()) && c.ControlType == "0").ToList();
                        }
                        if (nList.Count == 0)
                        {
                            nList = XChipCrl.Where(c => c.NameType.ToLower().Contains(item.Name.ToString().ToLower()) && c.ControlType == "0").ToList();
                        }
                        if (nList.Count == 1)
                        {
                            var Val = SerialSend.ReadRegister(Convert.ToUInt32(nList[0].Address, 16), sPortTemp).ToString("X");
                            item.Text = BitsHelp.GetBitsToUint(nList[0].Bits, nList[0].Hex, Val).ToString("X");
                        }
                        else
                        {
                            string Val = string.Empty;
                            //这里默认为是有多个寄存器  一个文本框的情况进行处理
                            if (nList.Exists(c => c.NameType.ToLower().Contains("_h")))
                            {
                                Val += SerialSend.ReadRegister(Convert.ToUInt32(nList.Find(c => c.NameType.ToLower().Contains("_h")).Address, 16), sPortTemp).ToString("X2");
                            }
                            if (nList.Exists(c => c.NameType.ToLower().Contains("_m1")))
                            {
                                Val += SerialSend.ReadRegister(Convert.ToUInt32(nList.Find(c => c.NameType.ToLower().Contains("_m1")).Address, 16), sPortTemp).ToString("X2");
                            }
                            if (nList.Exists(c => c.NameType.ToLower().Contains("_m0")))
                            {
                                Val += SerialSend.ReadRegister(Convert.ToUInt32(nList.Find(c => c.NameType.ToLower().Contains("_m0")).Address, 16), sPortTemp).ToString("X2");
                            }
                            if (nList.Exists(c => c.NameType.ToLower().Contains("_l")))
                            {
                                if (nList.Exists(c => c.NameType.ToLower().Substring(c.NameType.Length - 2).Contains("_l")))
                                {
                                    Val += SerialSend.ReadRegister(Convert.ToUInt32(nList.Find(c => c.NameType.ToLower().Substring(c.NameType.Length - 2).Contains("_l")).Address, 16), sPortTemp).ToString("X2");
                                }
                                else
                                {
                                    Val += SerialSend.ReadRegister(Convert.ToUInt32(nList.Last(c => c.NameType.ToLower().Contains("_l")).Address, 16), sPortTemp).ToString("X2");
                                }
                            }

                            item.Text = Val;
                        }
                    }
                    else if (item is CheckEdit)
                    {
                        var nList = XChipCrl.Where(c => c.NameType.ToLower().Contains(item.Name.ToString().ToLower()) && c.ControlType == "1").ToList();
                        if (nList.Count == 1)
                        {
                            var Val = SerialSend.ReadRegister(Convert.ToUInt32(nList[0].Address, 16), sPortTemp).ToString("X");
                            (item as CheckEdit).Checked = BitsHelp.GetBitsCheckedToBool(nList[0].Bits, nList[0].Hex, Val) == 0;
                        }
                    }
                    NameList.Add(item.Name);

                }

                //NameList.Add(type);
            }
            evenMask = false;
            WaitBefore.Close();

            #endregion

            #region 勾选框赋值
            evenMask = true;
            car_back_line_en.Checked = (GetCRLRegister("car_back_line_en") & 0x04) == 0x04;
            Curve_en.Checked = (GetCRLRegister("Curve_en") & 0x01) == 0x01;
            Zone5_ctrl.Checked = (GetCRLRegister("Zone5_ctrl") & 0x03) == 0x03;
            ///Portion_en 关系到多个左右勾选框  不能使用_en的读取方式  否则不能获取全部的值 导致勾选出错
            var model = XChipCrl.Find(c => c.NameType.ToLower() == "Portion_en".ToLower());
            var tempCheckMask = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
            //var tempCheckMask = GetCRLRegister("Portion_en");            
            Zone1_en.Checked = (tempCheckMask & 0xee) == 0xee;
            Zone2_en.Checked = (tempCheckMask & 0xdd) == 0xdd;
            Zone3_en.Checked = (tempCheckMask & 0xbb) == 0xbb;
            Zone4_en.Checked = (tempCheckMask & 0x77) == 0x77;


            EnShowHide();
            evenMask = false;
            #endregion
        }
        private Control[] Controls_Find(string str)
        {
            List<Control> listc = new List<Control>();
            System.Reflection.FieldInfo[] fieldInfo = this.GetType().GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

            for (int i = 0; i < fieldInfo.Length; i++)
            {
                if (fieldInfo[i].Name.ToLower().Contains(str.ToLower()) && !listc.Exists(c => c.Name.ToLower() == (fieldInfo[i].Name.ToLower())))
                {
                    listc.AddRange(this.Controls.Find(fieldInfo[i].Name, true));
                    //break;
                }
            }
            return listc.ToArray();
        }
        #region 调优Tips
        private void btn_Tips_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(threadPro);
            thread.Start();

        }

        private void threadPro()
        {
            MethodInvoker methodInvoker = new MethodInvoker(ShowFormB);
            BeginInvoke(methodInvoker);

        }

        private void ShowFormB()
        {
            //string str = "\t      \t 以横轴为X、景深为Y、纵轴为Z建立参考坐标系；\r\n"
            //+ "\t      \t 其中left_d1调整左侧Y方向线宽，left_d2调整X方向线宽，left_width1调整X方向水平偏移角度，left_width2调整左侧X方向线长，left_height调整Z方向垂直偏移高度，left_blank调整左侧区域线段间距；\r\n"
            //+ "\t      \t Left_x调整左侧X方向坐标，left_y调整左侧Y方向坐标，line color调整区域线段的yuv颜色；\r\n"
            //+ "\t      \t 右侧调整同理左侧；\r\n"
            //+ "\t      \t 更多详见《调试工具用户手册》。\r\n";
            string str = "";
            XChip_OptimizationTips ot = new XChip_OptimizationTips("AE调优Tips", str);
            ot.StartPosition = FormStartPosition.CenterScreen;
            //ot.ShowDialog();
            ot.Show();

        }
        #endregion

        #region Save_ISP_Setting
        private void btn_SaveIspSetting_Click(object sender, EventArgs e)
        {
            if (!sPortTemp.IsOpen && !IsUsbOpen && !m_open)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                return;
            }
            SaveFileDialog sfd = new SaveFileDialog();
            //设置文件类型 
            sfd.Filter = "配置文件（*.txt）|*.txt";

            //设置默认文件类型显示顺序 
            sfd.FilterIndex = 1;

            //保存对话框是否记忆上次打开的目录 
            sfd.RestoreDirectory = true;
            sfd.FileName = "IQ_Setting_CRL_" + DateTime.Now.ToString("yyyyMMddHHmms");
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                SerialSend.isDebug = false;
                string str = ParameterExport_CRL();
                SerialSend.isDebug = true;
                if (string.IsNullOrEmpty(str))
                {
                    return;
                }
                System.IO.File.WriteAllText(sfd.FileName, str, Encoding.Default);
                DevExpress.XtraEditors.XtraMessageBox.Show("参数导出完成");

            }
        }

        private string ParameterExport_CRL()
        {
            if (sPortTemp.IsOpen == false)
            {
                XtraMessageBox.Show("串口未打开", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
            //StringBuilder sb = new StringBuilder();
            if (null == Pe_dal)
            {
                Pe_dal = new parameter_exportManager();
            }
            if (null != Pe_dal)
            {
                WaitBefore.Start("参数导出", "正在加载中", 60);
                //var tempList = Pe_dal.GetList().OrderBy(c => c.SortNum);
                var tempList = Pe_dal.GetListToExport().Where(c => c.Module == "Car_back_line").OrderBy(c => c.SortNum).ToList();

                #region 白天寄存器
                var Pe_patch_dal = new parameter_export_patchManager();
                var patchList = Pe_patch_dal.GetList();
                var conditionList = tempList.Select(c => c.ConditionAddr).Distinct();
                foreach (var item in conditionList)
                {
                    if (SerialSend.ReadRegister(Convert.ToUInt32(item, 16), sPortTemp) == 0)
                    {
                        patchList.RemoveAll(c => c.ConditionAddr == item);
                    }
                }
                #endregion
                var sb = XChipHelp.ReadParameterExportExpand(tempList, "80", patchList, sPortTemp);
                //sb.Append("0xfffd,0x80,\n");
                //sb.Append($"//{tempList.First().Module}\n");
                //foreach (var item in tempList)
                //{
                //    try
                //    {
                //        sb.Append(string.IsNullOrEmpty(item.PreNote) ? "" : "\n" + item.PreNote + "\n");
                //        sb.Append(string.IsNullOrEmpty(item.addr) ? "" : item.addr);
                //        #region 判断是否需要用白天的数据
                //        if (!string.IsNullOrEmpty(item.ReadAddr))
                //        {
                //            if (patchList.Count != 0 && patchList.Select(c => c.ReadAddr).Contains(item.ReadAddr))
                //            {
                //                sb.Append($",0x{SerialSend.ReadRegister((Convert.ToUInt32(item.RelationAddr.Trim(), 16)), sPortTemp).ToString("x2")},");
                //            }
                //            else
                //            {
                //                sb.Append($",0x{SerialSend.ReadRegister((Convert.ToUInt32(item.ReadAddr.Trim(), 16)), sPortTemp).ToString("x2")},");
                //            }
                //        }
                //        #endregion
                //        sb.Append(string.IsNullOrEmpty(item.RearNote) ? "" : item.RearNote = item.RearNote.Replace("\\n", "\n").Replace("\\r", "\r"));
                //        sb.Append("\n");
                //    }
                //    catch (Exception ex)
                //    {
                //        WaitBefore.Close();
                //        //setShowMsg(item.RelationAddr);
                //        DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message);
                //        return null;
                //    }
                //}

                sb += ("\n//IQ_End\n");
                WaitBefore.Close();
                return sb.ToString();
            }
            return null;
        }
        #endregion

        #region 勾选框改变 下发对应寄存器
        private void check_car_back_line_en_CheckedChanged(object sender, EventArgs e)
        {
            if (!sPortTemp.IsOpen && !IsUsbOpen && !m_open)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                return;
            }
            if (evenMask)
                return;
            //var tempVal = GetCRLRegister("car_back_line_en");
            //if (car_back_line_en.Checked)
            //{
            //    SetCRLRegister("car_back_line_en", (uint)(tempVal | 0x04));
            //}
            //else
            //{
            //    SetCRLRegister("car_back_line_en", (uint)(tempVal & 0xfb));
            //}if (isRead) return;
            var txt = sender as CheckEdit;
            if (XChipCrl.Exists(c => c.NameType.ToLower() == (txt.Name).ToLower()))
            {
                var model = XChipCrl.Find(c => c.NameType.ToLower() == (txt.Name).ToLower());
                var Add = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), txt.Checked);
                SetCRLRegister(model, Val);
            }
        }

        private void check_Zone1_en_CheckedChanged(object sender, EventArgs e)
        {
            if (!sPortTemp.IsOpen && !IsUsbOpen && !m_open)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                return;
            }
            if (evenMask)
                return;
            var check = sender as CheckEdit;
            //var tempVal = GetCRLRegister("Portion_en");
            var model = XChipCrl.Find(c => c.NameType.ToLower() == "Portion_en".ToLower());
            var tempVal = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);

            if (check.Checked)
            {
                switch (Convert.ToUInt16(check.Tag))
                {
                    case 0:
                        SetCRLRegister("Portion_en", (uint)(tempVal | 0x11));
                        //if (XChipCrl.Exists(c => c.NameType.ToLower() == ($"Portion_en").ToLower()))
                        //{
                        //    var model = XChipCrl.Find(c => c.NameType.ToLower() == ($"Portion_en").ToLower());
                        //    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, (uint)(tempVal | 0x11));
                        //    SetCRLRegister(model, Val);
                        //}
                        break;
                    case 1:
                        SetCRLRegister("Portion_en", (uint)(tempVal | 0x22));
                        break;
                    case 2:
                        SetCRLRegister("Portion_en", (uint)(tempVal | 0x44));
                        break;
                    case 3:
                        SetCRLRegister("Portion_en", (uint)(tempVal | 0x88));
                        break;
                }
            }
            else
            {
                switch (Convert.ToUInt16(check.Tag))
                {
                    case 0:
                        SetCRLRegister("Portion_en", (uint)(tempVal & 0xee));
                        break;
                    case 1:
                        SetCRLRegister("Portion_en", (uint)(tempVal & 0xdd));
                        break;
                    case 2:
                        SetCRLRegister("Portion_en", (uint)(tempVal & 0xbb));
                        break;
                    case 3:
                        SetCRLRegister("Portion_en", (uint)(tempVal & 0x77));
                        break;
                }
            }
            EnShowHide();

        }
        #endregion

        #region 文本框键入回车下发







        private void txt_Zone1_y_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtTemp = sender as TextEdit;
                if (string.IsNullOrEmpty(txtTemp.Text))
                    return;
                //SetCRLRegister("Zone1_y", Convert.ToUInt16(txtTemp.Text, 16));
                if (XChipCrl.Exists(c => c.NameType.ToLower() == ($"Zone1_y").ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == ($"Zone1_y").ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Zone1_y.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void txt_Zone1_u_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtTemp = sender as TextEdit;
                if (string.IsNullOrEmpty(txtTemp.Text))
                    return;
                //SetCRLRegister("Zone1_u", Convert.ToUInt16(txtTemp.Text, 16));
                if (XChipCrl.Exists(c => c.NameType.ToLower() == ($"Zone1_u").ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == ($"Zone1_u").ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Zone1_u.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void txt_Zone1_v_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtTemp = sender as TextEdit;
                if (string.IsNullOrEmpty(txtTemp.Text))
                    return;
                //SetCRLRegister("Zone1_v", Convert.ToUInt16(txtTemp.Text, 16));
                if (XChipCrl.Exists(c => c.NameType.ToLower() == ($"Zone1_v").ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == ($"Zone1_v").ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Zone1_v.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void txt_Zone2_y_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtTemp = sender as TextEdit;
                if (string.IsNullOrEmpty(txtTemp.Text))
                    return;
                //SetCRLRegister("Zone2_y", Convert.ToUInt16(txtTemp.Text, 16));
                if (XChipCrl.Exists(c => c.NameType.ToLower() == ($"Zone2_y").ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == ($"Zone2_y").ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Zone2_y.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void txt_Zone2_u_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtTemp = sender as TextEdit;
                if (string.IsNullOrEmpty(txtTemp.Text))
                    return;
                //SetCRLRegister("Zone2_u", Convert.ToUInt16(txtTemp.Text, 16));
                if (XChipCrl.Exists(c => c.NameType.ToLower() == ($"Zone2_u").ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == ($"Zone2_u").ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Zone2_u.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void txt_Zone2_v_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtTemp = sender as TextEdit;
                if (string.IsNullOrEmpty(txtTemp.Text))
                    return;
                //SetCRLRegister("Zone2_v", Convert.ToUInt16(txtTemp.Text, 16));
                if (XChipCrl.Exists(c => c.NameType.ToLower() == ($"Zone2_v").ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == ($"Zone2_v").ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Zone2_v.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void txt_Zone3_y_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtTemp = sender as TextEdit;
                if (string.IsNullOrEmpty(txtTemp.Text))
                    return;
                //SetCRLRegister("Zone3_y", Convert.ToUInt16(txtTemp.Text, 16));
                if (XChipCrl.Exists(c => c.NameType.ToLower() == ($"Zone3_y").ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == ($"Zone3_y").ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Zone3_y.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void txt_Zone3_u_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtTemp = sender as TextEdit;
                if (string.IsNullOrEmpty(txtTemp.Text))
                    return;
                //SetCRLRegister("Zone3_u", Convert.ToUInt16(txtTemp.Text, 16));
                if (XChipCrl.Exists(c => c.NameType.ToLower() == ($"Zone3_u").ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == ($"Zone3_u").ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Zone3_u.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void txt_Zone3_v_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtTemp = sender as TextEdit;
                if (string.IsNullOrEmpty(txtTemp.Text))
                    return;
                //SetCRLRegister("Zone3_v", Convert.ToUInt16(txtTemp.Text, 16));
                if (XChipCrl.Exists(c => c.NameType.ToLower() == ($"Zone3_v").ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == ($"Zone3_v").ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Zone3_v.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void txt_Zone4_y_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtTemp = sender as TextEdit;
                if (string.IsNullOrEmpty(txtTemp.Text))
                    return;
                //SetCRLRegister("Zone4_y", Convert.ToUInt16(txtTemp.Text, 16));
                if (XChipCrl.Exists(c => c.NameType.ToLower() == ($"Zone4_y").ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == ($"Zone4_y").ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Zone4_y.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void txt_Zone4_u_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtTemp = sender as TextEdit;
                if (string.IsNullOrEmpty(txtTemp.Text))
                    return;
                //SetCRLRegister("Zone4_u", Convert.ToUInt16(txtTemp.Text, 16));
                if (XChipCrl.Exists(c => c.NameType.ToLower() == ($"Zone4_u").ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == ($"Zone4_u").ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Zone4_u.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void txt_Zone4_v_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtTemp = sender as TextEdit;
                if (string.IsNullOrEmpty(txtTemp.Text))
                    return;
                //SetCRLRegister("Zone4_v", Convert.ToUInt16(txtTemp.Text, 16));
                if (XChipCrl.Exists(c => c.NameType.ToLower() == ($"Zone4_v").ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == ($"Zone4_v").ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, Zone4_v.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }
        #endregion


        private void txt_Zone1_y_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            txt_Zone1_y_KeyDown(sender, keyEnter);
        }

        private void txt_Zone1_u_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            txt_Zone1_u_KeyDown(sender, keyEnter);
        }

        private void txt_Zone1_v_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            txt_Zone1_v_KeyDown(sender, keyEnter);
        }

        private void txt_Zone2_y_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            txt_Zone2_y_KeyDown(sender, keyEnter);
        }

        private void txt_Zone2_u_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            txt_Zone2_u_KeyDown(sender, keyEnter);
        }

        private void txt_Zone2_v_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            txt_Zone2_v_KeyDown(sender, keyEnter);
        }

        private void txt_Zone3_y_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            txt_Zone3_y_KeyDown(sender, keyEnter);
        }

        private void txt_Zone3_u_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            txt_Zone3_u_KeyDown(sender, keyEnter);
        }

        private void txt_Zone3_v_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            txt_Zone3_v_KeyDown(sender, keyEnter);
        }

        private void txt_Zone4_y_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            txt_Zone4_y_KeyDown(sender, keyEnter);
        }

        private void txt_Zone4_u_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            txt_Zone4_u_KeyDown(sender, keyEnter);
        }

        private void txt_Zone4_v_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            txt_Zone4_v_KeyDown(sender, keyEnter);
        }

        private void btn_GetIspSetting_MouseEnter(object sender, EventArgs e)
        {
            isWrite = true;
        }

        private void btn_GetIspSetting_MouseLeave(object sender, EventArgs e)
        {
            isWrite = false;
        }

        private void XChip_CRL_Load(object sender, EventArgs e)
        {

            Task.Run(() => {
                this.Invoke((EventHandler)delegate {

                    for (int i = 0; i < XChipCrl.Count; i++)
                    {

                        var type = AppHelper.NameTypeToString(XChipCrl[i].NameType);
                        //加上单选框的_en
                        if (XChipCrl[i].ControlType == "1")
                        {
                            type = XChipCrl[i].NameType;
                        }
                        try
                        {
                            //Control col = this.Controls.Find(type, true)[0];
                            var col = this.Controls.Find(type, true);


                            foreach (var item in col)
                            {
                                if (item is TextEdit)
                                {
                                    //声明一个SuerToolTip
                                    DevExpress.Utils.SuperToolTip graduationBatchSuperToolTip = new DevExpress.Utils.SuperToolTip();
                                    //声明一个ToolTipTitleItem,保存标题
                                    DevExpress.Utils.ToolTipTitleItem toolTipTitleItem = new DevExpress.Utils.ToolTipTitleItem();
                                    String tooltips = "";

                                    if (!string.IsNullOrEmpty(XChipCrl[i].Bit_Wide))
                                    {
                                        tooltips += $"位宽={XChipCrl[i].Bit_Wide}";
                                    }
                                    if (!string.IsNullOrEmpty(XChipCrl[i].Bit_Accuracy))
                                    {
                                        tooltips += $"|精度={XChipCrl[i].Bit_Accuracy}";
                                    }
                                    //设置标题文字
                                    toolTipTitleItem.Text = tooltips;
                                    //把标题添加到SuerToolTip
                                    graduationBatchSuperToolTip.Items.Add(toolTipTitleItem);
                                    //指定控件的SuperTip
                                    ((TextEdit)item).SuperTip = graduationBatchSuperToolTip;
                                    //正则判断
                                    ((TextEdit)item).Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.RegEx;
                                    ((TextEdit)item).Properties.Mask.EditMask = BitsHelp.GetBitsToMask(XChipCrl[i].Bits, XChipCrl[i].Bit_Wide, XChipCrl.Where(c => c.NameType.ToLower().Contains(type) && c.ControlType == "0").Select(c => c.Bits).ToList());
                                }
                            }
                        }
                        catch (Exception)
                        {
                            continue;
                        }
                    }
                });
            });
        }


        #region Left _Zone1

        private void Left_d1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtTemp = sender as TextEdit;
                if (string.IsNullOrEmpty(txtTemp.Text))
                    return;
                //SetCRLRegister("Left_d1", Convert.ToUInt16(txtTemp.Text, 16));
                if (XChipCrl.Exists(c => c.NameType.ToLower() == (txtTemp.Name).ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == (txtTemp.Name).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtTemp.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void Left_d1_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            Left_d1_KeyDown(sender, keyEnter);
        }

        private void Left_d2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtTemp = sender as TextEdit;
                if (string.IsNullOrEmpty(txtTemp.Text))
                    return;
                //SetCRLRegister("Left_d1", Convert.ToUInt16(txtTemp.Text, 16));
                if (XChipCrl.Exists(c => c.NameType.ToLower() == (txtTemp.Name).ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == (txtTemp.Name).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtTemp.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void Left_d2_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            Left_d2_KeyDown(sender, keyEnter);
        }

        private void Left_width2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtEdit = sender as TextEdit;
                string Temp = txtEdit.Text;
                if (string.IsNullOrEmpty(Temp))
                    return;
                if (Convert.ToUInt32(Temp, 16) > 0x7ff)
                {
                    txtEdit.Text = Temp = "7FF";
                }

                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void Left_width2_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            Left_width2_KeyDown(sender, keyEnter);
        }

        private void Left_height_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtEdit = sender as TextEdit;
                string Temp = txtEdit.Text;
                if (string.IsNullOrEmpty(Temp))
                    return;
                if (Convert.ToUInt32(Temp, 16) > 0x7ff)
                {
                    txtEdit.Text = Temp = "7FF";
                }

                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void Left_height_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            Left_height_KeyDown(sender, keyEnter);
        }

        private void Left_blank_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtEdit = sender as TextEdit;
                string Temp = txtEdit.Text;
                if (string.IsNullOrEmpty(Temp))
                    return;
                if (Convert.ToUInt32(Temp, 16) > 0x3ff)
                {
                    txtEdit.Text = Temp = "3FF";
                }

                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void Left_blank_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            Left_blank_KeyDown(sender, keyEnter);
        }



        #endregion

        #region Right _Zone1
        private void Right_d1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtTemp = sender as TextEdit;
                if (string.IsNullOrEmpty(txtTemp.Text))
                    return;
                //SetCRLRegister("Left_d1", Convert.ToUInt16(txtTemp.Text, 16));
                if (XChipCrl.Exists(c => c.NameType.ToLower() == (txtTemp.Name).ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == (txtTemp.Name).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtTemp.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void Right_d1_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            Right_d1_KeyDown(sender, keyEnter);
        }

        private void Right_d2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtTemp = sender as TextEdit;
                if (string.IsNullOrEmpty(txtTemp.Text))
                    return;
                //SetCRLRegister("Left_d1", Convert.ToUInt16(txtTemp.Text, 16));
                if (XChipCrl.Exists(c => c.NameType.ToLower() == (txtTemp.Name).ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == (txtTemp.Name).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtTemp.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void Right_d2_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            Right_d2_KeyDown(sender, keyEnter);
        }

        private void Right_width2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtEdit = sender as TextEdit;
                string Temp = txtEdit.Text;
                if (string.IsNullOrEmpty(Temp))
                    return;
                if (Convert.ToUInt32(Temp, 16) > 0x7ff)
                {
                    txtEdit.Text = Temp = "7FF";
                }

                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void Right_width2_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            Right_width2_KeyDown(sender, keyEnter);
        }

        private void Right_height_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtEdit = sender as TextEdit;
                string Temp = txtEdit.Text;
                if (string.IsNullOrEmpty(Temp))
                    return;
                if (Convert.ToUInt32(Temp, 16) > 0x7ff)
                {
                    txtEdit.Text = Temp = "7FF";
                }

                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void Right_height_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            Right_height_KeyDown(sender, keyEnter);
        }

        private void Right_blank_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtEdit = sender as TextEdit;
                string Temp = txtEdit.Text;
                if (string.IsNullOrEmpty(Temp))
                    return;
                if (Convert.ToUInt32(Temp, 16) > 0x3ff)
                {
                    txtEdit.Text = Temp = "3FF";
                }

                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void Right_blank_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            Right_blank_KeyDown(sender, keyEnter);
        }

        #endregion

        #region Left_Zone2
        private void left_zone2_d1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtTemp = sender as TextEdit;
                if (string.IsNullOrEmpty(txtTemp.Text))
                    return;
                //SetCRLRegister("Left_d1", Convert.ToUInt16(txtTemp.Text, 16));
                if (XChipCrl.Exists(c => c.NameType.ToLower() == (txtTemp.Name).ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == (txtTemp.Name).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtTemp.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void left_zone2_d1_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            left_zone2_d1_KeyDown(sender, keyEnter);
        }

        private void left_zone2_d2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtTemp = sender as TextEdit;
                if (string.IsNullOrEmpty(txtTemp.Text))
                    return;
                //SetCRLRegister("Left_d1", Convert.ToUInt16(txtTemp.Text, 16));
                if (XChipCrl.Exists(c => c.NameType.ToLower() == (txtTemp.Name).ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == (txtTemp.Name).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtTemp.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void left_zone2_d2_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            left_zone2_d2_KeyDown(sender, keyEnter);
        }

        private void left_zone2_width2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtEdit = sender as TextEdit;
                string Temp = txtEdit.Text;
                if (string.IsNullOrEmpty(Temp))
                    return;
                if (Convert.ToUInt32(Temp, 16) > 0x7ff)
                {
                    txtEdit.Text = Temp = "7FF";
                }

                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void left_zone2_width2_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            left_zone2_width2_KeyDown(sender, keyEnter);
        }

        private void left_zone2_height_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtEdit = sender as TextEdit;
                string Temp = txtEdit.Text;
                if (string.IsNullOrEmpty(Temp))
                    return;
                if (Convert.ToUInt32(Temp, 16) > 0x7ff)
                {
                    txtEdit.Text = Temp = "7FF";
                }

                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void left_zone2_height_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            left_zone2_height_KeyDown(sender, keyEnter);
        }

        private void zone2_left_blank_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtEdit = sender as TextEdit;
                string Temp = txtEdit.Text;
                if (string.IsNullOrEmpty(Temp))
                    return;
                if (Convert.ToUInt32(Temp, 16) > 0x3ff)
                {
                    txtEdit.Text = Temp = "3FF";
                }

                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void zone2_left_blank_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            zone2_left_blank_KeyDown(sender, keyEnter);
        }
        #endregion

        #region Right_Zone2
        private void right_zone2_d1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtTemp = sender as TextEdit;
                if (string.IsNullOrEmpty(txtTemp.Text))
                    return;
                //SetCRLRegister("Left_d1", Convert.ToUInt16(txtTemp.Text, 16));
                if (XChipCrl.Exists(c => c.NameType.ToLower() == (txtTemp.Name).ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == (txtTemp.Name).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtTemp.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void right_zone2_d1_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            right_zone2_d1_KeyDown(sender, keyEnter);
        }

        private void right_zone2_d2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtTemp = sender as TextEdit;
                if (string.IsNullOrEmpty(txtTemp.Text))
                    return;
                //SetCRLRegister("Left_d1", Convert.ToUInt16(txtTemp.Text, 16));
                if (XChipCrl.Exists(c => c.NameType.ToLower() == (txtTemp.Name).ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == (txtTemp.Name).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtTemp.Text);
                    SetCRLRegister(model, Val);
                }
            }

        }

        private void right_zone2_d2_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            right_zone2_d2_KeyDown(sender, keyEnter);
        }

        private void right_zone2_width2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtEdit = sender as TextEdit;
                string Temp = txtEdit.Text;
                if (string.IsNullOrEmpty(Temp))
                    return;
                if (Convert.ToUInt32(Temp, 16) > 0x7ff)
                {
                    txtEdit.Text = Temp = "7FF";
                }

                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void right_zone2_width2_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            right_zone2_width2_KeyDown(sender, keyEnter);
        }

        private void right_zone2_height_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtEdit = sender as TextEdit;
                string Temp = txtEdit.Text;
                if (string.IsNullOrEmpty(Temp))
                    return;
                if (Convert.ToUInt32(Temp, 16) > 0x7ff)
                {
                    txtEdit.Text = Temp = "7FF";
                }

                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void right_zone2_height_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            right_zone2_height_KeyDown(sender, keyEnter);
        }

        private void zone2_right_blank_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtEdit = sender as TextEdit;
                string Temp = txtEdit.Text;
                if (string.IsNullOrEmpty(Temp))
                    return;
                if (Convert.ToUInt32(Temp, 16) > 0x3ff)
                {
                    txtEdit.Text = Temp = "3FF";
                }

                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void zone2_right_blank_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            zone2_right_blank_KeyDown(sender, keyEnter);
        }

        #endregion

        #region Left_Zone3 

        private void left_zone3_d1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtTemp = sender as TextEdit;
                if (string.IsNullOrEmpty(txtTemp.Text))
                    return;
                //SetCRLRegister("Left_d1", Convert.ToUInt16(txtTemp.Text, 16));
                if (XChipCrl.Exists(c => c.NameType.ToLower() == (txtTemp.Name).ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == (txtTemp.Name).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtTemp.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void left_zone3_d1_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            left_zone3_d1_KeyDown(sender, keyEnter);
        }

        private void left_zone3_d2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtTemp = sender as TextEdit;
                if (string.IsNullOrEmpty(txtTemp.Text))
                    return;
                //SetCRLRegister("Left_d1", Convert.ToUInt16(txtTemp.Text, 16));
                if (XChipCrl.Exists(c => c.NameType.ToLower() == (txtTemp.Name).ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == (txtTemp.Name).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtTemp.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void left_zone3_d2_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            left_zone3_d2_KeyDown(sender, keyEnter);
        }

        private void left_zone3_width2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtEdit = sender as TextEdit;
                string Temp = txtEdit.Text;
                if (string.IsNullOrEmpty(Temp))
                    return;
                if (Convert.ToUInt32(Temp, 16) > 0x7ff)
                {
                    txtEdit.Text = Temp = "7FF";
                }

                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void left_zone3_width2_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            left_zone3_width2_KeyDown(sender, keyEnter);
        }

        private void left_zone3_height_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtEdit = sender as TextEdit;
                string Temp = txtEdit.Text;
                if (string.IsNullOrEmpty(Temp))
                    return;
                if (Convert.ToUInt32(Temp, 16) > 0x7ff)
                {
                    txtEdit.Text = Temp = "7FF";
                }

                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void left_zone3_height_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            left_zone3_height_KeyDown(sender, keyEnter);
        }

        private void zone3_left_blank_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtEdit = sender as TextEdit;
                string Temp = txtEdit.Text;
                if (string.IsNullOrEmpty(Temp))
                    return;
                if (Convert.ToUInt32(Temp, 16) > 0x3ff)
                {
                    txtEdit.Text = Temp = "3FF";
                }

                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void zone3_left_blank_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            zone3_left_blank_KeyDown(sender, keyEnter);
        }


        #endregion

        #region Right_Zone3
        private void right_zone3_d1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtTemp = sender as TextEdit;
                if (string.IsNullOrEmpty(txtTemp.Text))
                    return;
                //SetCRLRegister("Left_d1", Convert.ToUInt16(txtTemp.Text, 16));
                if (XChipCrl.Exists(c => c.NameType.ToLower() == (txtTemp.Name).ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == (txtTemp.Name).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtTemp.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void right_zone3_d1_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            right_zone3_d1_KeyDown(sender, keyEnter);
        }

        private void right_zone3_d2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtTemp = sender as TextEdit;
                if (string.IsNullOrEmpty(txtTemp.Text))
                    return;
                //SetCRLRegister("Left_d1", Convert.ToUInt16(txtTemp.Text, 16));
                if (XChipCrl.Exists(c => c.NameType.ToLower() == (txtTemp.Name).ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == (txtTemp.Name).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtTemp.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void right_zone3_d2_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            right_zone3_d2_KeyDown(sender, keyEnter);
        }

        private void right_zone3_width2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtEdit = sender as TextEdit;
                string Temp = txtEdit.Text;
                if (string.IsNullOrEmpty(Temp))
                    return;
                if (Convert.ToUInt32(Temp, 16) > 0x7ff)
                {
                    txtEdit.Text = Temp = "7FF";
                }

                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void right_zone3_width2_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            right_zone3_width2_KeyDown(sender, keyEnter);
        }

        private void right_zone3_height_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtEdit = sender as TextEdit;
                string Temp = txtEdit.Text;
                if (string.IsNullOrEmpty(Temp))
                    return;
                if (Convert.ToUInt32(Temp, 16) > 0x7ff)
                {
                    txtEdit.Text = Temp = "7FF";
                }

                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void right_zone3_height_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            right_zone3_height_KeyDown(sender, keyEnter);
        }

        private void zone3_right_blank_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtEdit = sender as TextEdit;
                string Temp = txtEdit.Text;
                if (string.IsNullOrEmpty(Temp))
                    return;
                if (Convert.ToUInt32(Temp, 16) > 0x3ff)
                {
                    txtEdit.Text = Temp = "3FF";
                }

                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void zone3_right_blank_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            zone3_right_blank_KeyDown(sender, keyEnter);
        }

        #endregion

        #region Left_Zone4
        private void left_zone4_d1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtTemp = sender as TextEdit;
                if (string.IsNullOrEmpty(txtTemp.Text))
                    return;
                //SetCRLRegister("Left_d1", Convert.ToUInt16(txtTemp.Text, 16));
                if (XChipCrl.Exists(c => c.NameType.ToLower() == (txtTemp.Name).ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == (txtTemp.Name).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtTemp.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void left_zone4_d1_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            left_zone4_d1_KeyDown(sender, keyEnter);
        }

        private void left_zone4_d2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtTemp = sender as TextEdit;
                if (string.IsNullOrEmpty(txtTemp.Text))
                    return;
                //SetCRLRegister("Left_d1", Convert.ToUInt16(txtTemp.Text, 16));
                if (XChipCrl.Exists(c => c.NameType.ToLower() == (txtTemp.Name).ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == (txtTemp.Name).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtTemp.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void left_zone4_d2_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            left_zone4_d2_KeyDown(sender, keyEnter);
        }

        private void left_zone4_width2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtEdit = sender as TextEdit;
                string Temp = txtEdit.Text;
                if (string.IsNullOrEmpty(Temp))
                    return;
                if (Convert.ToUInt32(Temp, 16) > 0x7ff)
                {
                    txtEdit.Text = Temp = "7FF";
                }

                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void left_zone4_width2_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            left_zone4_width2_KeyDown(sender, keyEnter);
        }

        private void left_zone4_height_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtEdit = sender as TextEdit;
                string Temp = txtEdit.Text;
                if (string.IsNullOrEmpty(Temp))
                    return;
                if (Convert.ToUInt32(Temp, 16) > 0x7ff)
                {
                    txtEdit.Text = Temp = "7FF";
                }

                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void left_zone4_height_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            left_zone4_height_KeyDown(sender, keyEnter);
        }

        #endregion

        #region Right_Zone4
        private void right_zone4_d1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtTemp = sender as TextEdit;
                if (string.IsNullOrEmpty(txtTemp.Text))
                    return;
                //SetCRLRegister("Left_d1", Convert.ToUInt16(txtTemp.Text, 16));
                if (XChipCrl.Exists(c => c.NameType.ToLower() == (txtTemp.Name).ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == (txtTemp.Name).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtTemp.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void right_zone4_d1_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            right_zone4_d1_KeyDown(sender, keyEnter);
        }

        private void right_zone4_d2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtTemp = sender as TextEdit;
                if (string.IsNullOrEmpty(txtTemp.Text))
                    return;
                //SetCRLRegister("Left_d1", Convert.ToUInt16(txtTemp.Text, 16));
                if (XChipCrl.Exists(c => c.NameType.ToLower() == (txtTemp.Name).ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == (txtTemp.Name).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtTemp.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void right_zone4_d2_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            right_zone4_d2_KeyDown(sender, keyEnter);
        }

        private void right_zone4_width2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtEdit = sender as TextEdit;
                string Temp = txtEdit.Text;
                if (string.IsNullOrEmpty(Temp))
                    return;
                if (Convert.ToUInt32(Temp, 16) > 0x7ff)
                {
                    txtEdit.Text = Temp = "7FF";
                }

                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void right_zone4_width2_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            right_zone4_width2_KeyDown(sender, keyEnter);
        }

        private void right_zone4_height_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtEdit = sender as TextEdit;
                string Temp = txtEdit.Text;
                if (string.IsNullOrEmpty(Temp))
                    return;
                if (Convert.ToUInt32(Temp, 16) > 0x7ff)
                {
                    txtEdit.Text = Temp = "7FF";
                }

                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void right_zone4_height_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            right_zone4_height_KeyDown(sender, keyEnter);
        }

        #endregion

        #region Left
        private void Left_width1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtEdit = sender as TextEdit;
                string Temp = txtEdit.Text;
                if (string.IsNullOrEmpty(Temp))
                    return;
                if (Convert.ToUInt32(Temp, 16) > 0x7ff)
                {
                    txtEdit.Text = Temp = "7FF";
                }

                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void Left_width1_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            Left_width1_KeyDown(sender, keyEnter);
        }

        private void Left_x_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtEdit = sender as TextEdit;
                string Temp = txtEdit.Text;
                if (string.IsNullOrEmpty(Temp))
                    return;
                if (Convert.ToUInt32(Temp, 16) > 0xfff)
                {
                    txtEdit.Text = Temp = "fFF";
                }

                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void Left_x_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            Left_x_KeyDown(sender, keyEnter);
        }

        private void Left_y_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtEdit = sender as TextEdit;
                string Temp = txtEdit.Text;
                if (string.IsNullOrEmpty(Temp))
                    return;
                if (Convert.ToUInt32(Temp, 16) > 0x7ff)
                {
                    txtEdit.Text = Temp = "7FF";
                }

                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void Left_y_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            Left_y_KeyDown(sender, keyEnter);
        }

        #endregion

        #region Right
        private void Right_width1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtEdit = sender as TextEdit;
                string Temp = txtEdit.Text;
                if (string.IsNullOrEmpty(Temp))
                    return;
                if (Convert.ToUInt32(Temp, 16) > 0x7ff)
                {
                    txtEdit.Text = Temp = "7FF";
                }

                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void Right_width1_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            Right_width1_KeyDown(sender, keyEnter);
        }

        private void Right_x_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtEdit = sender as TextEdit;
                string Temp = txtEdit.Text;
                if (string.IsNullOrEmpty(Temp))
                    return;
                if (Convert.ToUInt32(Temp, 16) > 0xfff)
                {
                    txtEdit.Text = Temp = "fFF";
                }

                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void Right_x_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            Right_x_KeyDown(sender, keyEnter);
        }

        private void Right_y_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtEdit = sender as TextEdit;
                string Temp = txtEdit.Text;
                if (string.IsNullOrEmpty(Temp))
                    return;
                if (Convert.ToUInt32(Temp, 16) > 0x7ff)
                {
                    txtEdit.Text = Temp = "7FF";
                }

                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void Right_y_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            Right_y_KeyDown(sender, keyEnter);
        }

        #endregion

        #region Zone5
        private void Zone5_ctrl_CheckedChanged(object sender, EventArgs e)
        {
            if (!sPortTemp.IsOpen && !IsUsbOpen && !m_open)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                return;
            }
            if (evenMask)
                return;
            if (XChipCrl.Exists(c => c.NameType.ToLower() == ($"Zone5_ctrl").ToLower()))
            {
                var model = XChipCrl.Find(c => c.NameType.ToLower() == ($"Zone5_ctrl").ToLower());
                var Add = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), Zone5_ctrl.Checked);
                SetCRLRegister(model, Val);
            }
        }

        private void Zone5_width_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtTemp = sender as TextEdit;
                if (string.IsNullOrEmpty(txtTemp.Text))
                    return;
                //SetCRLRegister("Left_d1", Convert.ToUInt16(txtTemp.Text, 16));
                if (XChipCrl.Exists(c => c.NameType.ToLower() == (txtTemp.Name).ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == (txtTemp.Name).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtTemp.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void Zone5_width_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            Zone5_width_KeyDown(sender, keyEnter);

        }

        private void Zone5_height_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtTemp = sender as TextEdit;
                if (string.IsNullOrEmpty(txtTemp.Text))
                    return;
                //SetCRLRegister("Left_d1", Convert.ToUInt16(txtTemp.Text, 16));
                if (XChipCrl.Exists(c => c.NameType.ToLower() == (txtTemp.Name).ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == (txtTemp.Name).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtTemp.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void Zone5_height_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            Zone5_height_KeyDown(sender, keyEnter);
        }
        #endregion

        #region Curve
        private void Curve_en_CheckedChanged(object sender, EventArgs e)
        {
            if (!sPortTemp.IsOpen && !IsUsbOpen && !m_open)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("可用端口未打开");
                return;
            }
            if (evenMask)
                return;
            if (XChipCrl.Exists(c => c.NameType.ToLower() == ($"Curve_en").ToLower()))
            {
                var model = XChipCrl.Find(c => c.NameType.ToLower() == ($"Curve_en").ToLower());
                var Add = SerialSend.ReadRegister(Convert.ToUInt32(model.Address, 16), sPortTemp);
                var Val = BitsHelp.SetBitsCheckedToUint(model.Bits, model.Hex, Add.ToString("X2"), Curve_en.Checked);
                SetCRLRegister(model, Val);
            }
            EnShowHide();
        }

        private void Curve_midx_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtEdit = sender as TextEdit;
                string Temp = txtEdit.Text;
                if (string.IsNullOrEmpty(Temp))
                    return;
                if (Convert.ToUInt32(Temp, 16) > 0x7ff)
                {
                    txtEdit.Text = Temp = "7FF";
                }

                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void Curve_midx_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            Curve_midx_KeyDown(sender, keyEnter);
        }

        private void Curve_midy_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtEdit = sender as TextEdit;
                string Temp = txtEdit.Text;
                if (string.IsNullOrEmpty(Temp))
                    return;
                if (Convert.ToUInt32(Temp, 16) > 0x7ff)
                {
                    txtEdit.Text = Temp = "7FF";
                }

                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_h".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
                if (XChipCrl.Exists(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == $"{txtEdit.Name}_l".ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtEdit.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void Curve_midy_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            Curve_midy_KeyDown(sender, keyEnter);
        }

        private void Curve_height_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtTemp = sender as TextEdit;
                if (string.IsNullOrEmpty(txtTemp.Text))
                    return;
                //SetCRLRegister("Left_d1", Convert.ToUInt16(txtTemp.Text, 16));
                if (XChipCrl.Exists(c => c.NameType.ToLower() == (txtTemp.Name).ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == (txtTemp.Name).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtTemp.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void Curve_height_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            Curve_height_KeyDown(sender, keyEnter);
        }

        private void Curve_curvature_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtTemp = sender as TextEdit;
                if (string.IsNullOrEmpty(txtTemp.Text))
                    return;
                //SetCRLRegister("Left_d1", Convert.ToUInt16(txtTemp.Text, 16));
                if (XChipCrl.Exists(c => c.NameType.ToLower() == (txtTemp.Name).ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == (txtTemp.Name).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtTemp.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void Curve_curvature_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            Curve_curvature_KeyDown(sender, keyEnter);
        }

        private void Curve_y_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtTemp = sender as TextEdit;
                if (string.IsNullOrEmpty(txtTemp.Text))
                    return;
                //SetCRLRegister("Left_d1", Convert.ToUInt16(txtTemp.Text, 16));
                if (XChipCrl.Exists(c => c.NameType.ToLower() == (txtTemp.Name).ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == (txtTemp.Name).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtTemp.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void Curve_y_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            Curve_y_KeyDown(sender, keyEnter);
        }

        private void Curve_u_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtTemp = sender as TextEdit;
                if (string.IsNullOrEmpty(txtTemp.Text))
                    return;
                //SetCRLRegister("Left_d1", Convert.ToUInt16(txtTemp.Text, 16));
                if (XChipCrl.Exists(c => c.NameType.ToLower() == (txtTemp.Name).ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == (txtTemp.Name).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtTemp.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void Curve_u_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            Curve_u_KeyDown(sender, keyEnter);
        }

        private void Curve_v_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextEdit txtTemp = sender as TextEdit;
                if (string.IsNullOrEmpty(txtTemp.Text))
                    return;
                //SetCRLRegister("Left_d1", Convert.ToUInt16(txtTemp.Text, 16));
                if (XChipCrl.Exists(c => c.NameType.ToLower() == (txtTemp.Name).ToLower()))
                {
                    var model = XChipCrl.Find(c => c.NameType.ToLower() == (txtTemp.Name).ToLower());
                    var Val = BitsHelp.SetBitsToUint(model.Bits, model.Hex, txtTemp.Text);
                    SetCRLRegister(model, Val);
                }
            }
        }

        private void Curve_v_Leave(object sender, EventArgs e)
        {
            if (isWrite) return;
            Curve_v_KeyDown(sender, keyEnter);
        }
        private void EnShowHide()
        {
            Zone1_y.Enabled = Zone1_en.Checked;
            Zone1_u.Enabled = Zone1_en.Checked;
            Zone1_v.Enabled = Zone1_en.Checked;
            Zone2_y.Enabled = Zone2_en.Checked;
            Zone2_u.Enabled = Zone2_en.Checked;
            Zone2_v.Enabled = Zone2_en.Checked;
            Zone3_y.Enabled = Zone3_en.Checked;
            Zone3_u.Enabled = Zone3_en.Checked;
            Zone3_v.Enabled = Zone3_en.Checked;
            Zone4_y.Enabled = Zone4_en.Checked;
            Zone4_u.Enabled = Zone4_en.Checked;
            Zone4_v.Enabled = Zone4_en.Checked;
            Curve_y.Enabled = Curve_en.Checked;
            Curve_u.Enabled = Curve_en.Checked;
            Curve_v.Enabled = Curve_en.Checked;
            Zone5_width.Enabled = Zone5_ctrl.Checked;
            Zone5_height.Enabled = Zone5_ctrl.Checked;
        }

        #endregion

    }
}
